//
//  AuthViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//  提现到微信

import UIKit

class AuthViewController: BaseViewController,inresponse {

    let wx = WXLogin.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.defaultBackButton()
        self.title = "提现到微信"
        configView()
        // Do any additional setup after loading the view.
    }

    func configView()->Void{
        self.view.backgroundColor = UIColor.white
        let imageView = UIImageView.init(frame: CGRect.init(x: YXDeviceWidth / 2 - 70 * YXWidthScale, y: 86 * YXHeightScale, width: 140 * YXWidthScale, height: 172 * YXHeightScale))
        imageView.image = UIImage.load(name: "icon_wechat")
        self.view.addSubview(imageView)
        let font = UIFont.systemFont(ofSize: 40 * YXWidthScale)
        let size = "微信".StringSize(font: font)
        let cnt = UILabel.init()
        cnt.frame = CGRect.init(x: YXDeviceWidth / 2 - size.width / 2, y: imageView.bottom(), width: size.width, height: size.height)
        cnt.textColor = UIColor.black
        cnt.font = font
        cnt.text = "微信"
        self.view.addSubview(cnt)
        
        let line = UIView.init()
        line.frame = CGRect.init(x: 60 * YXWidthScale, y: cnt.bottom() + 80 * YXHeightScale, width: YXDeviceWidth - 120 * YXWidthScale, height: YXHeightScale)
        line.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.view.addSubview(line)
        
        let txt = "说明:为了您的帐户安全，根据微信支付要求，“回收趣”需要获取您的微信号，将钱款打到您的微信钱包，您的隐私信息不会泄漏，请放心授权。"
        let attributed = NSMutableAttributedString.init(string: txt)
        let font1 = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        attributed.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.colorWithHexString(hex: "646464"),NSAttributedStringKey.font : font1], range: NSRange.init(location: 0, length: attributed.length))
        attributed.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.black], range: NSRange.init(location: 0, length: 3))
        let label = UILabel.init()
        label.frame = CGRect.init(origin: CGPoint.init(x: line.left(), y: line.bottom() + 58 * YXHeightScale), size: CGSize.zero)
        label.attributedText = attributed
        label.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        label.configLineSpacing(space: 48 * YXHeightScale)
        self.view.addSubview(label)
        
        let button = UIButton.init(type: .system)
        button.addTarget(self, action: #selector(authLogin), for: .touchUpInside)
        button.backgroundColor = UIColor.black
        button.setTitleColor(UIColor.white, for: .normal)
        button.layer.cornerRadius = YXButtonCorner
        button.titleLabel?.font = UIFont.systemFont(ofSize: 36 * YXWidthScale)
        button.setTitle("授权登录", for: .normal)
        button.frame = CGRect.init(origin: CGPoint.init(x: (YXDeviceWidth - 630 * YXWidthScale) / 2, y: label.bottom() + 80 * YXHeightScale), size: CGSize.init(width: 630 * YXWidthScale, height: 88 * YXHeightScale))
        self.view.addSubview(button)
    }
    
    @objc func authLogin() -> Void {
        wx.load(viewCtl: self, scope: "snsapi_userinfo", state: "enghou", openid: WXAPIKey, delet: self)
    }
    
    
    
    
    func response(urls: String) {
        self.requestInfo(urls: urls)
    }
    
    func errorOccur(response: SendAuthResp) {
        //print(response)
    }
    
    func requestInfo(urls : String) -> Void {
        let u = URL.init(string: urls)!
        let session = URLSession.shared
        let task1 = session.dataTask(with: u, completionHandler: {(data,response,error) in
            do{
                let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as? Dictionary<String,Any>
                //将openid传给后台
                if let openid = dic!["openid"] as? String {
                    //存储并传递给后台
                    let vc = AlipayAuthViewController.init()
                    vc.cash_account = openid
                    vc.type = 1
                    if let nickname = dic!["nickname"] as? String{
                        vc.nick_nm = nickname
                    }
                    DispatchQueue.main.async {
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }catch{
                
            }
        })
        task1.resume()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
